package com.suredy.app.porject.model.sp;

import java.text.DecimalFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import com.suredy.app.porject.constants.UserFileType;
import com.suredy.core.model.BaseModel;

/**
 * 投标文件模型
 * 
 * @author VIVID.G
 * @since 2015-10-12
 * @version v0.1
 */
@MappedSuperclass
public class UserFile extends BaseModel {

	private static final long serialVersionUID = 1L;

	@Column(name = "type", length = 50, nullable = false)
	@Enumerated(EnumType.STRING)
	private UserFileType type;

	/* 上传时间 */
	@Column(name = "upload_time", nullable = false)
	private Date uploadTime;

	/* 文件的实际名称 */
	@Column(name = "file_name", length = 200, nullable = false)
	private String fileName;

	/* 文件大小 */
	@Column(name = "file_size", nullable = false)
	private Long size;

	/* 文件后缀 */
	@Column(name = "file_fuffix", nullable = false)
	private String suffix;

	/* 文件上传人 */
	@Column(name = "upload_user", length = 50, nullable = false)
	private String userName;

	/*  */
	@Column(name = "file_path", length = 500, nullable = false)
	private String filePath;

	/* 备注 */
	@Column(length = 3000)
	private String remark;

	public UserFileType getType() {
		return type;
	}

	public void setType(UserFileType type) {
		this.type = type;
	}

	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Long getSize() {
		return size;
	}

	public void setSize(Long size) {
		this.size = size;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	// ========== 以下是业务方法 ========== //

	@Transient
	public String getFileSizeString() {
		if (this.getSize() == null)
			return "";

		DecimalFormat df = new DecimalFormat("0.##");

		float b = 1f; // B
		float k = b * 1024; // 1K
		float m = k * 1024; // 1M
		float g = m * 1024; // 1G
		float t = g * 1024; // 1T

		float val = 0f;

		if ((val = this.getSize() / t) > 1) {
			return df.format(val) + "T";
		}

		if ((val = this.getSize() / g) > 1) {
			return df.format(val) + "G";
		}

		if ((val = this.getSize() / m) > 1) {
			return df.format(val) + "M";
		}

		if ((val = this.getSize() / k) > 1) {
			return df.format(val) + "K";
		}

		return this.getSize() + "B";
	}

}
